import 'package:eleapp/res/strings.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

// 控制器
import '../../controller/usercontroller.dart';
// 路由
import '../login/login.dart';
// 状态管理
import '../../localstorage/storage.dart';

class Personal extends StatefulWidget {
  const Personal({super.key});

  @override
  State<Personal> createState() => _PersonalState();
}

class _PersonalState extends State<Personal> {
  UserController userController = Get.find();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          UserAccountsDrawerHeader(
            accountName: Obx(() => Text("${userController.user['name']}")),
            accountEmail: Obx(() => Text("${userController.user['mobile']}")),
            currentAccountPicture: GestureDetector(
              onTap: () {
                Get.toNamed('/photopreview', arguments: {
                  'imageList': [userController.user['avatar']],
                  'curIndex': 0,
                  'isAvatar': true
                });
              },
              child: Hero(
                  tag: 'img0',
                  child: Obx(() => CircleAvatar(
                        backgroundImage:
                            NetworkImage("${userController.user['avatar']}"),
                      ))),
            ),
            decoration: const BoxDecoration(
              image: DecorationImage(
                  image: NetworkImage(
                      "https://www.itying.com/images/flutter/2.png"),
                  fit: BoxFit.cover),
            ),
          ),
          ListTile(
            title: Text(StringStyles.personalcenter.tr),
            leading: const CircleAvatar(child: Icon(Icons.people)),
            onTap: () async {
              // 初始化用户本地存储操作类
              Store store = await Store.getInstance();
              final loginstatus = await store.getBoolStatus('islogin');
              final user = store.getMap('user');

              print('点击个人中心');
              print(loginstatus);
              print(user);

              Get.toNamed('/myinfo');
            },
          ),
          const Divider(),
          ListTile(
            title: Text(StringStyles.setting.tr),
            leading: const CircleAvatar(child: Icon(Icons.settings)),
            onTap: () {
              Get.toNamed('/settings');
            },
          ),
          const Divider(),
          ListTile(
            title: Text(StringStyles.loginout.tr),
            leading: const CircleAvatar(child: Icon(Icons.logout)),
            onTap: () async {
              // 清除本地缓存
              Store store = await Store.getInstance();
              await store.remove(StringStyles.userAccount);
              await store.remove(StringStyles.userId);
              await store.remove(StringStyles.userInfo);
              await store.remove(StringStyles.islogin);

              // 清除全局数据
              userController.user.value = {};
              userController.userId.value = 0;
              userController.userAccount.value = '';
              userController.islogin.value = false;

              Get.offAll(() => const Login());

              // replace 方法
              // Navigator.pushNamedAndRemoveUntil(
              //     context, '/login', (route) => false);
            },
          ),
          const Divider(),
        ],
      ),
    );
  }
}
